package com.walk.zworld.ui

import android.content.Intent
import android.os.Bundle
import android.view.View
import android.view.animation.AlphaAnimation
import android.view.animation.Animation
import android.view.animation.ScaleAnimation
import android.widget.ImageView
import android.widget.TextView
import com.walk.zworld.R
import com.base.lib.base.utils.MyMMKV
import com.base.lib.base.utils.ThreadUtils
import com.base.lib.base.view.BaseActivity
import com.base.lib.common.utils.DateUtil
import com.base.lib.common.utils.SdkVersionUtil
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.text.SimpleDateFormat

/**
 * 闪屏页，可以放入广告等页面
 */
class SplashActivity : BaseActivity() {

    companion object {
        /**
         * 是否首次进入APP应用
         */
        var isFirstEntryApp: Boolean
            get() = MyMMKV.mmkv.decodeBool("is_first_entry_app", true)
            set(value) {
                CoroutineScope(Dispatchers.IO).launch { MyMMKV.mmkv.encode("is_first_entry_app", value) }
            }
        var isToDayFirstEntryApp: Boolean
            get() {
                val today = DateUtil.getToday();
                return MyMMKV.mmkv.decodeBool("is_today_first_entry_app_$today", true)
            }
            set(value) {
                val today = DateUtil.getToday();
                CoroutineScope(Dispatchers.IO).launch { MyMMKV.mmkv.encode("is_today_first_entry_app_$today", value) }
            }
    }

    private var top_time: TextView? = null
    private var topTime: Int = 5
    private var splashDuration = 5 * 1000L

    private val alphaAnimation by lazy {
        AlphaAnimation(0.1f, 1.0f).apply {
            duration = splashDuration/2
            fillAfter = true
        }
    }

    private val scaleAnimation by lazy {
        ScaleAnimation(1f, 1.1f, 1f, 1.1f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f).apply {
            duration = splashDuration
            fillAfter = true
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        if (!isToDayFirstEntryApp) {
            topTime = 1
            splashDuration = 1 * 1000L
        }
        super.onCreate(savedInstanceState)
        isFirstEntryApp = false
        isToDayFirstEntryApp = false
    }

    override fun onBindLayout(): Int = R.layout.activity_splash

    override fun initView() {
        findViewById<TextView>(R.id.ivSlogan).startAnimation(alphaAnimation)
        findViewById<ImageView>(R.id.ivSloganBg).startAnimation(scaleAnimation)
        top_time = findViewById<TextView>(R.id.top_time)
        top_time?.text = topTime.toString()
        if (topTime != 1) top_time?.visibility = View.VISIBLE

        findViewById<TextView>(R.id.now_go).setOnClickListener {
            topTime = -1;
            gotoMainActivity()
        }
        top_time?.setOnClickListener {
            topTime = -1;
            gotoMainActivity()
        }
    }

    override fun initData() {
        ThreadUtils.runOnUiThread({
            if (topTime == -1) {
                return@runOnUiThread
            }
            topTime -= 1
            if (topTime <= 0){
                gotoMainActivity()
            }else {
                top_time?.text = topTime.toString()
                initData();
            }
            if (topTime < 3){
                findViewById<TextView>(R.id.now_go).visibility = View.VISIBLE
            }
        },1000)
    }

    private fun gotoMainActivity() {
        if (SdkVersionUtil.getAppVersion(mContext) <= 5) {
            startActivity(Intent(this, Main1Activity::class.java))
        } else {
            startActivity(Intent(this, MainActivity::class.java))
        }
        finish()
    }
}